#coding=utf8
#import requests
import itchat
import get_data
# from apscheduler.schedulers.blocking import BlockingScheduler

def get_response():
    sql = 'select content,type from every_report order by id desc limit 1' 
    res = get_data.select_db(db, sql)
    print(res[0][1])
    return res

def send_all():
    res_reply = get_response()
    if res_reply[0][1] == 1:
        itchat.send_msg(res_reply[0][0], toUserName='filehelper')
    else:
        itchat.send('@img@%s' % res_reply[0][0], toUserName='filehelper')     
    # friendList = itchat.get_friends(update=False)###获取好友列表
    # print(friendList)
    # for friend in friendList:
    #     i+=1
    #     print('第%d个    '%(i),WANT_TO_SAY % (friend['DisplayName'] or friend['NickName']))
    #itchat.send(WANT_TO_SAY % (friend['DisplayName']or friend['NickName']))
def after_login():
    scheduler.add_job(func=send_all, trigger='cron', hour=8,minute=0,second=5)
    scheduler.start()

def after_logout():
    sched.shutdown()

# @itchat.msg_register(itchat.content.TEXT)
# def tuling_reply(msg):
#     defaultReply = 'I received: ' + msg['Text']
#     reply = get_response()
#     return reply or defaultReply


if __name__ == "__main__" :
    global db
    db = get_data.connect_mysql()

    # global scheduler
    # scheduler = BlockingScheduler()
    
    itchat.auto_login(hotReload=True, enableCmdQR=2, loginCallback=after_login, exitCallback=after_logout)
    itchat.run()